package com.light.applet.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.light.applet.account.mapper.AccountMapper;
import com.light.applet.account.service.AccountService;
import com.light.applet.common.entity.Account;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * 用户表(Account)表服务实现类
 *
 * @author weibiao
 * @since 2024-04-18 21:35:05
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Override
    public Account register(Account account) {
        LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Account::getOpenId, account.getOpenId());
        Account existsAccount = getOne(queryWrapper);
        if (ObjectUtils.isEmpty(existsAccount)) {
            Account wantingAccount = Account.builder()
                    .openId(account.getOpenId())
                    .createTime(new Date())
                    .build();
            save(wantingAccount);
            return wantingAccount;
        }
        return existsAccount;
    }
}

